import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import * as cookieParser from 'cookie-parser';
import * as session from 'express-session';
import { AppModule } from './app.module';
import { LoggerInterceptor } from './interceptor/logger.interceptor';
import { HttpExceptionFilter } from './filter/http-exception.filter';
import { LogService } from './views/log/log.service';


// 扩展 Express Session 接口
declare module 'express-session' {
  interface SessionData {
    code: string;
  }
}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(
    session({
      secret: 'nest-admin',
      name: "flowertip.session", 
      rolling: true,
    }),
  );
  app.use(cookieParser());
  app.setGlobalPrefix('api'); // 设置全局前缀

  // 配置全局验证管道
  app.useGlobalPipes(new ValidationPipe({
    whitelist: true, // 自动过滤未定义的属性
    forbidNonWhitelisted: true, // 禁止未定义的属性
    transform: true, // 自动转换类型
  }));

  // 配置全局拦截器和异常过滤器
  const logService = app.get(LogService);
  app.useGlobalInterceptors(new LoggerInterceptor(logService));
  app.useGlobalFilters(new HttpExceptionFilter(logService));

  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
